/*
  Doesn't draw zero lenght strings (use for place holders)
  start a line with a '_' to dim that line by dimTextAlpha
  [example:
  Hilighted\n
  _dimmed 1\n
  _dimmed 2
  [/example]
*/

class TextCloud {

// PARAMETERS
  static final float popUpSize = 40;
  static final float popInSize = 10;

  final float popUpStrenght = 1;
  final float popInStrenght = 0.2;

  final float popUpDamping = 5.0;
  final float popInDamping = 1.0;
// PARAMETERS END

// MEMBERS
  Text[] txt;
  int currentText = -1;
  ParticleSystem textPs;
// MEMBERS END

  public TextCloud() {

    textPs = new ParticleSystem(0, 0);
    txt = new Text[slides.length];

    for(int i=0; i < slides.length; i++) {
      String rowText = slides[i];
      Particle center = null;

      float a = random(TWO_PI);
      float len = random(150);
      float startx = sin(a) * len;
      float starty = cos(a) * len;

      float stopx = startx;
      float stopy = starty;

      if(rowText.startsWith("+")) {
        rowText = rowText.substring(1, rowText.length());
        stopx = 0; stopy = 0;
      }

      // create the text and the particle
      txt[i] = new Text(rowText);
      txt[i].p = textPs.makeParticle(2, startx, starty, popInSize);

      txt[i].inX = startx; txt[i].inY = starty; txt[i].inZ = popInSize;
      txt[i].outX = stopx; txt[i].outY = stopy; txt[i].outZ = popUpSize;
    }
  }

  public void nextSlide() {
    if(currentText >= 0)
      txt[currentText].popIn();
    currentText = (currentText + 1) % slides.length;
    txt[currentText].popUp();

    println("Showing slide " + currentText);
  }

  public void prevSlide() {
    if(currentText >= 0)
      txt[currentText].popIn();
    currentText = currentText - 1;
    if(currentText < 0) currentText = 0;
    txt[currentText].popUp();

    println("Showing slide " + currentText);
  }


  public void gotoStart() {
    if(currentText >= 0)
      txt[currentText].popIn();
    currentText = -1; // next will be + 1 = 0
    println("Restarting slides");
  }

  public void update() {
    textPs.advanceTime(1);
    float dx, dy, dz;

    // turn off pullinSpring when close enough
    for(int i=0; i < slides.length; i++) {

      float destX = txt[i].isPopped ? txt[i].outX : txt[i].inX;
      float destY = txt[i].isPopped ? txt[i].outY : txt[i].inY;
      float destZ = txt[i].isPopped ? txt[i].outZ : txt[i].inZ;

      float mx = 0, my = 0, mz = 0;

      float dist = dist(
          txt[i].x(), txt[i].y(), txt[i].z(),
          destX, destY, destZ);

      float mv = 5;

      dx = txt[i].x() - destX;
      dy = txt[i].y() - destY;
      dz = txt[i].z() - destZ;
      
      txt[i].p.moveBy(- (dx / mv), -(dy / mv), -(dz / mv));
    }
  }

  public void draw() {
    textAlign(CENTER, TOP);

    noStroke();
    fill(backgroundColor, 200);
    rectMode(CORNERS);
    rect(-width/2, -height/2, width/2, height/2);

    // draw unpopped first
    for(int i=0; i < slides.length; i++) {
      if(!txt[i].isPopped) txt[i].draw();
    }
    // overlay with popped
    for(int i=0; i < slides.length; i++) {
      if(txt[i].isPopped) txt[i].draw();
    }
  }
}

